package com.woniuxy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.entity.MBlog;
import com.woniuxy.service.BlogService;
import com.woniuxy.util.FileUtil;
import com.woniuxy.util.JwtUtils;
import com.woniuxy.util.ResultVo;
import io.jsonwebtoken.Jwt;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Slf4j
@RestController
@Transactional
public class BlogController {
    @Autowired
    BlogService blogService;

    //上传图片
    @PostMapping("/blog/uploadImg")
    public ResultVo uploadFile(@RequestParam("file") MultipartFile file) throws InvalidPortException, InvalidEndpointException, IOException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //获得当前的文件名称  1.jpg
        String filename = file.getOriginalFilename();
        //获得后缀名 .jpg
        String suffix = filename.substring(filename.lastIndexOf("."));
        //获得新的文件名，要不重复
        filename = UUID.randomUUID()+suffix;
        //调用minioAPI 选桶存储
        MinioClient minioClient = new MinioClient("http://localhost:9000/","minioadmin","minioadmin");
        PutObjectOptions options = new PutObjectOptions(file.getSize(),1000000000);
        options.setContentType(file.getContentType());
        minioClient.putObject("assets",filename,file.getInputStream(),options);
        //返回图片地址 http://localhost:8090/note/assets/image-***.png
        String uploadFilename = "http://localhost:9000/"+"assets/"+filename;
        return ResultVo.success("上传成功",uploadFilename);
    }

    @PostMapping("blog/post")
    public ResultVo postBlog(@RequestBody HashMap<String,Object> map) {
        log.info(map.toString());
        String token = (String) map.remove("token");
        Map<String,Object> bMap = (Map<String, Object>) map.remove("blog");

        MBlog blog = new MBlog();
        blog.setTitle((String) bMap.remove("title"));
        blog.setDescription((String) bMap.remove("description"));
        String content = (String) bMap.remove("content");
        String s = content.substring(content.indexOf("(")+1);
        String s1 = s.substring(0, s.lastIndexOf(")"));
        blog.setContent(s1);

        String loginId = JwtUtils.getLoginId(token);
        int i = blogService.postBlog(loginId, blog);
        return ResultVo.success();
    }

    @PostMapping("blog/del")
    public ResultVo delBlog(@RequestBody String id) {
        return ResultVo.success();
    }

    @PostMapping("blog/search")
    public ResultVo searchBlog(@RequestBody String token) {
        log.info("token==========" + token);
        String loginId = JwtUtils.getLoginId(token);
        log.info("取出token中的Id" + loginId);
        List search = blogService.search(Integer.valueOf(loginId));
        return ResultVo.success(search);
    }
}
